package com.example.calendarapp;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
 
/**
 * 
 * @author Emilio
 *  the other swipe implementor to allow the programs to swipe between fragment
 *  takes an touch action links it to a direction which moves the screen in that direction 
 *  Detects when swipe distance is too small so the user doesn't randomly move the screen
 *  all fragments are able to be accessed by swiping
 *  
 */

public class OtherSwipeDetector implements View.OnTouchListener {
 
    public static enum Action { 
        LR, // Left to Right
        RL, // Right to Left
        TB, // Top to bottom
        BT, // Bottom to Top
        None // when no action was detected
    } 
 
    private static final String logTag = "SwipeDetector";
    private static final int MIN_DISTANCE = 100;
    private static final int HORIZONTAL_MIN_DISTANCE = 160;
    private static final int VERTICAL_MIN_DISTANCE = 80;
    private float downX, downY, upX, upY;
    private Action mSwipeDetected = Action.None;
 
    /**
     *  // tell the program the user has swiped 
     *  with a boolean returning ture when works
     * @return bool
     */
    
    public boolean swipeDetected() { 
        return mSwipeDetected != Action.None;
    } 
    
    /** gets the action decected and sends it to the returns when it works
     * 
     * @return swipe
     */
 
    public Action getAction() { 
        return mSwipeDetected;
    } 
 
    /**
     * the main method of the class
     * goes uses a switch statement to determine which direction was pulled
     * prevents certain movements such as up and down which are not integrated into the program from working
     * if a swipe doesn't meet the minimum requirements then it will nor perform the action
     * it is set up so that the minimum direction you move is based on where you placed your finger rather then a set distance from center
     * only set up to work with horizontal
     * 
     * @param v
     * @param event
     * @return boolean
     */
    
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                downX = event.getX();
                downY = event.getY();
                mSwipeDetected = Action.None; 
                return false; // allow other events like Click to be processed 
            } 
            case MotionEvent.ACTION_UP: {
                upX = event.getX();
                upY = event.getY();
     
                float deltaX = downX - upX;
                float deltaY = downY - upY;
     
                // horizontal swipe detection 
                if (Math.abs(deltaX) > HORIZONTAL_MIN_DISTANCE) {
                    // left or right 
                    if (deltaX < 0) {
                    	right();
                    	mSwipeDetected = Action.LR; 
                        return true; 
                    } 
                    if (deltaX > 0) {
                    	left();
                    	mSwipeDetected = Action.RL; 
                        return true; 
                    } 
                } else  
     
                // vertical swipe detection 
                if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE) {
                    // top or down 
                    if (deltaY < 0) {
                        Log.i(logTag, "Swipe Top to Bottom");
                        mSwipeDetected = Action.TB; 
                        return false; 
                    } 
                    if (deltaY > 0) {
                        Log.i(logTag, "Swipe Bottom to Top");
                        mSwipeDetected = Action.BT; 
                        return false; 
                    } 
                }  
                return false; 
            } 
        } 
        return false; 
    } 
    
    /**
     * Empty method to be overridden by class 
     *
     */
    
    public void left()
    {
    	
    }
  
    /**
     * Empty method to be overridden by class 
     *
     */
    
    public void right()
    {
    	
    }
    
} 

